import bs4
import re
import urllib.request
import xlwt
import sqlite3
import urllib.error
import urllib.request
import urllib.parse
import ssl
import time
ssl._create_default_https_context = ssl._create_unverified_context
from bs4 import BeautifulSoup


from prettytable import PrettyTable
import pandas as pdd
import json

import hashlib
import sys

def p(all="",introduction=""):
    # print("-------"+introduction+"---------")
    print(all)
    # print("-------end---------")
    print("--------"+introduction)


def dfh(df):
    html = df.to_html()
    head="<!DOCTYPE html><html><head><title>Paul version ^ ^</title><meta charset='utf-8'></head><style>td{padding: 5px;text-align: left;}thead{background-color:royalblue;color: cornsilk;}th{text-align: center;padding: 5px;}tr:nth-child(even)  td  {background-color:lightskyblue;}tr:nth-child(odd)  td{background-color:#ffffff;}body{font-size:14px}</style><body>"
    tail="</body></html>"
    html=html.replace("<table", "<table cellpadding=0 cellspacing=0")
    html=head+html+tail
    with open('a.html', 'w') as f:
        f.write(html)

def geoh(geo):
    geo.render('a.html')

def d():
    sys.exit(0)
    # raise Exception("-----------die------------")

def pd(all=""):
    p(all)
    d()

def sbls(ols=list()):
    dic=dict()
    for k,v in enumerate(ols):
        dic[k]=v
    return dic

def dfp(df=pdd.DataFrame(),hd=0):
    if hasattr(df, 'columns')==False:
        print(df.values)
        dic = {"0":df.values}
        df = pdd.DataFrame(dic)

    if hd!=0:
        df=df.head(hd)

    clms=df.columns
    ls=list()
    ls.append("")
    for v in clms:
        ls.append(v)
    x = PrettyTable(ls)

    x.padding_width = 1
    idxs=df.index

    for v in idxs:
        ls=list()
        ls.append(v)
        for kk,vv in df.loc[v].items():
            ls.append(vv)
        x.add_row(ls)
    print(x)

def je(dic):
    jsn=json.dumps(dic)
    return jsn
def jd(jsn):
    dic=json.loads(s=jsn)
    return dic
def headline(df):
    return df.columns.values.tolist()


# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.



def vurl(url):
    html = askurl(url)
    bs = BeautifulSoup(html, "html.parser")
    return bs

    # workbook.save("abcd.xls")
def askurl(url):
    headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}
    rqst = urllib.request.Request(url=url, headers=headers)
    html=""
    try:
        rsp = urllib.request.urlopen(rqst)
        html=rsp.read().decode("utf-8")

    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    return html


def fullnum(num,needwei):
    numwei=len(str(num))
    s=""
    for i in range(needwei-numwei):
        s=s+"0"
    return s+str(num)

def md5(s):
    return hashlib.md5(s.encode(encoding='UTF-8')).hexdigest()

def df2ls(df):
    all_ls=list()
    for idx,sr in df.iterrows():
        s_ls=list()
        # 遍历series
        for idx2,v2 in sr.items():
            s_ls.append(v2)
        all_ls.append(s_ls)
    return all_ls

def tm(tp=-1,nyr="",tmstmp=0):
    t=time.time()
    if tp==-1:
        t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(t))
        return t
    elif tp==1:
        return int(t)
    elif tp==2:
        timeArray = time.strptime(nyr, "%Y-%m-%d %H:%M:%S")
        # 转换成时间戳
        timestamp = time.mktime(timeArray)
        return timestamp
    elif tp==3:
        t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(tmstmp))
        return t

def df2ls(df):
    return df.values.tolist()



    